package test.test.test;
/*
 * THis program is supposed to create a menu with a background and buttons that go to the game, options, and quit
 */
import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.scene.menu.MenuScene;
import org.anddev.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.anddev.andengine.entity.scene.menu.item.IMenuItem;
import org.anddev.andengine.entity.scene.menu.item.TextMenuItem;
import org.anddev.andengine.entity.scene.menu.item.decorator.ColorMenuItemDecorator;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.font.FontFactory;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import test.test.R;

import android.content.Intent;
import android.graphics.Color;


public class MainMenu extends BaseGameActivity implements IOnMenuItemClickListener {
	// ===========================================================
	// Constants
	// ===========================================================

	private static final int CAMERA_WIDTH = 720;
	private static final int CAMERA_HEIGHT = 480;

	//Creates Id's for the buttons
	protected static final int MENU_GAME1 = 0;
	protected static final int MENU_OPTIONS = MENU_GAME1 + 3;
	protected static final int MENU_QUIT = MENU_GAME1 + 4;

	// ===========================================================
	// Fields
	// ===========================================================

	protected Camera mCamera;

	protected Scene mMainScene;

	private Texture mFontTexture;
	private Font mFont;

	protected MenuScene mMenuScene;

	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	public Engine onLoadEngine() {
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);		
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,
				new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
	}
	public void onLoadResources() {
		/* Load Font/Textures. */
		this.mFontTexture = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		FontFactory.setAssetBasePath("font/");
		this.mFont = FontFactory.createFromAsset(this.mFontTexture, this, "Zrnic.ttf", 52, true, Color.WHITE);
		this.mEngine.getTextureManager().loadTexture(this.mFontTexture);
		this.mEngine.getFontManager().loadFont(this.mFont);

	}

	public Scene onLoadScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());
				
		//Initializes the Menu
		this.mMenuScene = this.createMenuScene();

		//Creates the Scene and sets  the background
		this.mMainScene = new Scene();
		this.mMainScene.setBackground(new ColorBackground(1f, 1f, 1f));
		
		//Puts the menu in the scene
		this.mMainScene.setChildScene(this.mMenuScene, false, true, true);
		
		return this.mMainScene;
	}

/*
 * (non-Javadoc)
 * @see org.anddev.andengine.ui.IGameInterface#onLoadComplete()
 * What happens after the resources have been loaded
 */
	public void onLoadComplete() {

	}

	//This controls the function of the back Key
	/*@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			//If back is pressed than nothing happens
			return true; 
		} else { 
			return super.onKeyDown( keyCode, event); 
		} 			
	}*/

	public boolean onMenuItemClicked(final MenuScene pMenuScene, final IMenuItem pMenuItem, final float pMenuItemLocalX, final float pMenuItemLocalY) {
		switch(pMenuItem.getID()) {
			case MENU_GAME1:
				/* Start Game Activity.*/
				Intent intent1=new Intent(MainMenu.this, Game.class); 
				startActivity(intent1);
				return true;
			case MENU_OPTIONS:
				/* Start Game Activity. */
				Intent intent4=new Intent(MainMenu.this, Options.class); 
				startActivity(intent4);
				return true;
			case MENU_QUIT:
				/* End Activity. */
				this.finish();
				return true;
			default:
				return false;
		}
	}

	// ===========================================================
	// Methods
	// ===========================================================
	/*
	 * Creates the menu as you see it, which gets passed to on load scene
	 * You set the variable for text, then font, and then the string that is seen on the screen. You then set the color for the font
	 */
	protected MenuScene createMenuScene() {
		final MenuScene menuScene = new MenuScene(this.mCamera);
		//Does not do anything
		menuScene.setPosition(10, 10);
		final IMenuItem Game1MenuItem = new ColorMenuItemDecorator(new TextMenuItem(MENU_GAME1, this.mFont, "Game1"), 1.0f,0.0f,0.0f, 0.0f,0.0f,0.0f);
		Game1MenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		menuScene.addMenuItem(Game1MenuItem);
		
		final IMenuItem OptionsMenuItem = new ColorMenuItemDecorator(new TextMenuItem(MENU_OPTIONS, this.mFont, "Options"), 1.0f,0.0f,0.0f, 0.0f,0.0f,0.0f);
		OptionsMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		menuScene.addMenuItem(OptionsMenuItem);
		
		final IMenuItem quitMenuItem = new ColorMenuItemDecorator(new TextMenuItem(MENU_QUIT, this.mFont, "Quit"), 1.0f,0.0f,0.0f, 0.0f,0.0f,0.0f);
		quitMenuItem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		menuScene.addMenuItem(quitMenuItem);

		menuScene.buildAnimations();

		menuScene.setBackgroundEnabled(false);

		menuScene.setOnMenuItemClickListener(this);
		return menuScene;
	}


	// ==========================================================
	// Inner and Anonymous Classes
	// ===========================================================

	//Transitions done when an activity is pushed to the background
	@Override
	protected void onPause() {
		super.onPause();
		MainMenu.this.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
	}
}







